package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.common.MyException;
import com.itheima.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/1 15:15
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 添加
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("添加成功！");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Category>> page(int page, int pageSize) {
        IPage<Category> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        IPage<Category> page1 = categoryService.page(iPage, wrapper);
        return R.success(page1);
    }

    /**
     * 修改
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功！");
    }

    /**
     * 删除,如果查询到的分类下有菜品的话就不允许删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        Category category = categoryService.getById(id);
        Integer type = category.getType();
        if (type == 1) {
            // 是菜品分类
            QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id", id);
            // 查询菜品表
            int result = dishService.count(queryWrapper);
            if (result > 0) {
                // 抛出自定义的异常(先被全局异常处理器拦截，再由全局异常处理器处理)
                throw new MyException("该菜品分类下有菜品，不能删除！");
//                return R.error("该菜品分类下有菜品，不能删除！");
            }
        } else {
            // 是套餐分类
            QueryWrapper<Setmeal> querrywrapper = new QueryWrapper<>();
            querrywrapper.eq("category_id", id);
            int result = setmealService.count(querrywrapper);
            if (result > 0) {
                throw new MyException("该菜品分类下有菜品，不能删除！！");
//                return R.error("该菜品分类下有菜品，不能删除！");
            }
        }
        categoryService.removeById(id);
        // 如果条件不满足就说明，分类下没有菜品可以删除
        return R.success("删除成功");
    }

    /**
     * 添加菜品的菜品分类
     *
     * @return
     */
    @GetMapping("/list")
    public R listType(Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType() != null ,Category::getType, category.getType());
        List<Category> categoryList = categoryService.list(queryWrapper);
        return R.success(categoryList);

    }


}
